//-----------------------------------------------------------------------
// <copyright file="a_OrderDAOBase.cs" company="AACECOM">
//     Copyright (c) AACECOM. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

using System;
using System.Data;
using System.Data.Common;
using System.Collections;
using Microsoft.Practices.EnterpriseLibrary.Data;
using ThoiTrang.Entities;

namespace ThoiTrang.Data
{    
    public abstract class a_OrderDAOBase
    {
        #region Common methods
        public virtual a_Order Createa_OrderFromReader(IDataReader reader)
        {
            a_Order item = new a_Order();
            try
            {
                if (!reader.IsDBNull(reader.GetOrdinal("OID"))) item.OID = (int)reader["OID"];
                if (!reader.IsDBNull(reader.GetOrdinal("DomSaleOrderNo"))) item.DomSaleOrderNo = (string)reader["DomSaleOrderNo"];
                if (!reader.IsDBNull(reader.GetOrdinal("CustomerID_UserID"))) item.CustomerID_UserID = (int)reader["CustomerID_UserID"];
                if (!reader.IsDBNull(reader.GetOrdinal("Billing_AddrID"))) item.Billing_AddrID = (int)reader["Billing_AddrID"];
                if (!reader.IsDBNull(reader.GetOrdinal("Shipping_AddrID"))) item.Shipping_AddrID = (int)reader["Shipping_AddrID"];
                if (!reader.IsDBNull(reader.GetOrdinal("OrderDate"))) item.OrderDate = (DateTime)reader["OrderDate"];
                if (!reader.IsDBNull(reader.GetOrdinal("ModifiedDate"))) item.ModifiedDate = (DateTime)reader["ModifiedDate"];
                if (!reader.IsDBNull(reader.GetOrdinal("ShipMethodID"))) item.ShipMethodID = (int)reader["ShipMethodID"];
                if (!reader.IsDBNull(reader.GetOrdinal("ShippingOptions"))) item.ShippingOptions = (bool)reader["ShippingOptions"];
                if (!reader.IsDBNull(reader.GetOrdinal("InstrumentType"))) item.InstrumentType = (byte)reader["InstrumentType"];
                if (!reader.IsDBNull(reader.GetOrdinal("InstrumentID"))) item.InstrumentID = (int)reader["InstrumentID"];
                if (!reader.IsDBNull(reader.GetOrdinal("SubTotal"))) item.SubTotal = (decimal)reader["SubTotal"];
                if (!reader.IsDBNull(reader.GetOrdinal("Tax"))) item.Tax = (decimal)reader["Tax"];
                if (!reader.IsDBNull(reader.GetOrdinal("PromoTaxCredit"))) item.PromoTaxCredit = (decimal)reader["PromoTaxCredit"];
                if (!reader.IsDBNull(reader.GetOrdinal("Shipping_Weight"))) item.Shipping_Weight = (int)reader["Shipping_Weight"];
                if (!reader.IsDBNull(reader.GetOrdinal("Shipping_Handling"))) item.Shipping_Handling = (decimal)reader["Shipping_Handling"];
                if (!reader.IsDBNull(reader.GetOrdinal("PromoShippingCredit"))) item.PromoShippingCredit = (decimal)reader["PromoShippingCredit"];
                if (!reader.IsDBNull(reader.GetOrdinal("ShipChargeNotIncluded"))) item.ShipChargeNotIncluded = (bool)reader["ShipChargeNotIncluded"];
                if (!reader.IsDBNull(reader.GetOrdinal("CouponCredit"))) item.CouponCredit = (decimal)reader["CouponCredit"];
                if (!reader.IsDBNull(reader.GetOrdinal("UserCredit"))) item.UserCredit = (decimal)reader["UserCredit"];
                if (!reader.IsDBNull(reader.GetOrdinal("CODCharge"))) item.CODCharge = (decimal)reader["CODCharge"];
                if (!reader.IsDBNull(reader.GetOrdinal("Total"))) item.Total = (decimal)reader["Total"];
                if (!reader.IsDBNull(reader.GetOrdinal("Stage"))) item.Stage = (string)reader["Stage"];
                if (!reader.IsDBNull(reader.GetOrdinal("Status"))) item.Status = (string)reader["Status"];
                if (!reader.IsDBNull(reader.GetOrdinal("StatusPayment"))) item.StatusPayment = (byte)reader["StatusPayment"];
                if (!reader.IsDBNull(reader.GetOrdinal("StatusShipping"))) item.StatusShipping = (byte)reader["StatusShipping"];
                if (!reader.IsDBNull(reader.GetOrdinal("BackOrderCount"))) item.BackOrderCount = (int)reader["BackOrderCount"];
                if (!reader.IsDBNull(reader.GetOrdinal("IsDelete"))) item.IsDelete = (bool)reader["IsDelete"];
                if (!reader.IsDBNull(reader.GetOrdinal("DomainID"))) item.DomainID = (int)reader["DomainID"];
                if (!reader.IsDBNull(reader.GetOrdinal("ApprovalCode"))) item.ApprovalCode = (string)reader["ApprovalCode"];
                if (!reader.IsDBNull(reader.GetOrdinal("TransID"))) item.TransID = (string)reader["TransID"];
                if (!reader.IsDBNull(reader.GetOrdinal("AVSCode"))) item.AVSCode = (string)reader["AVSCode"];
                if (!reader.IsDBNull(reader.GetOrdinal("SecCode"))) item.SecCode = (string)reader["SecCode"];
                if (!reader.IsDBNull(reader.GetOrdinal("NumOfRetry"))) item.NumOfRetry = (int)reader["NumOfRetry"];
                if (!reader.IsDBNull(reader.GetOrdinal("PayPal_PayerEmail"))) item.PayPal_PayerEmail = (string)reader["PayPal_PayerEmail"];
                if (!reader.IsDBNull(reader.GetOrdinal("IsPending"))) item.IsPending = (bool)reader["IsPending"];
                if (!reader.IsDBNull(reader.GetOrdinal("HasNote"))) item.HasNote = (bool)reader["HasNote"];
                if (!reader.IsDBNull(reader.GetOrdinal("ShipAsGift"))) item.ShipAsGift = (bool)reader["ShipAsGift"];
                if (!reader.IsDBNull(reader.GetOrdinal("Discount"))) item.Discount = (decimal)reader["Discount"];
                if (!reader.IsDBNull(reader.GetOrdinal("CouponValueType"))) item.CouponValueType = (bool)reader["CouponValueType"];
                if (!reader.IsDBNull(reader.GetOrdinal("CouponValue"))) item.CouponValue = (int)reader["CouponValue"];
                if (!reader.IsDBNull(reader.GetOrdinal("Typecoupon"))) item.Typecoupon = (int)reader["Typecoupon"];
                if (!reader.IsDBNull(reader.GetOrdinal("CouponID"))) item.CouponID = (string)reader["CouponID"];
                if (!reader.IsDBNull(reader.GetOrdinal("NofDiscount"))) item.NofDiscount = (int)reader["NofDiscount"];
                if (!reader.IsDBNull(reader.GetOrdinal("IsDeclined"))) item.IsDeclined = (bool)reader["IsDeclined"];
                if (!reader.IsDBNull(reader.GetOrdinal("UserID_Approved"))) item.UserID_Approved = (int)reader["UserID_Approved"];
                if (!reader.IsDBNull(reader.GetOrdinal("AVSZip"))) item.AVSZip = (string)reader["AVSZip"];
                if (!reader.IsDBNull(reader.GetOrdinal("ProcessStatus"))) item.ProcessStatus = (int)reader["ProcessStatus"];
                if (!reader.IsDBNull(reader.GetOrdinal("MerchantStatus"))) item.MerchantStatus = (int)reader["MerchantStatus"];
                if (!reader.IsDBNull(reader.GetOrdinal("LotteryWon"))) item.LotteryWon = (bool)reader["LotteryWon"];
                if (!reader.IsDBNull(reader.GetOrdinal("LotteryNum"))) item.LotteryNum = (string)reader["LotteryNum"];
            }
            catch (Exception ex)
            {                
                throw ex;
            }
            return item;
        }
        #endregion
        
        #region Createa_Order methods
            
        public virtual void Createa_Order(a_Order a_Order)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_Orders_ins");
                
                database.AddInParameter(dbCommand, "@DomSaleOrderNo", DbType.AnsiString, a_Order.DomSaleOrderNo);
                database.AddInParameter(dbCommand, "@CustomerID_UserID", DbType.Int32, a_Order.CustomerID_UserID);
                database.AddInParameter(dbCommand, "@Billing_AddrID", DbType.Int32, a_Order.Billing_AddrID);
                database.AddInParameter(dbCommand, "@Shipping_AddrID", DbType.Int32, a_Order.Shipping_AddrID);
                database.AddInParameter(dbCommand, "@OrderDate", DbType.DateTime, a_Order.OrderDate);
                database.AddInParameter(dbCommand, "@ModifiedDate", DbType.DateTime, a_Order.ModifiedDate);
                database.AddInParameter(dbCommand, "@ShipMethodID", DbType.Int32, a_Order.ShipMethodID);
                database.AddInParameter(dbCommand, "@ShippingOptions", DbType.Boolean, a_Order.ShippingOptions);
                database.AddInParameter(dbCommand, "@InstrumentType", DbType.Byte, a_Order.InstrumentType);
                database.AddInParameter(dbCommand, "@InstrumentID", DbType.Int32, a_Order.InstrumentID);
                database.AddInParameter(dbCommand, "@SubTotal", DbType.Currency, a_Order.SubTotal);
                database.AddInParameter(dbCommand, "@Tax", DbType.Currency, a_Order.Tax);
                database.AddInParameter(dbCommand, "@PromoTaxCredit", DbType.Currency, a_Order.PromoTaxCredit);
                database.AddInParameter(dbCommand, "@Shipping_Weight", DbType.Int32, a_Order.Shipping_Weight);
                database.AddInParameter(dbCommand, "@Shipping_Handling", DbType.Currency, a_Order.Shipping_Handling);
                database.AddInParameter(dbCommand, "@PromoShippingCredit", DbType.Currency, a_Order.PromoShippingCredit);
                database.AddInParameter(dbCommand, "@ShipChargeNotIncluded", DbType.Boolean, a_Order.ShipChargeNotIncluded);
                database.AddInParameter(dbCommand, "@CouponCredit", DbType.Currency, a_Order.CouponCredit);
                database.AddInParameter(dbCommand, "@UserCredit", DbType.Currency, a_Order.UserCredit);
                database.AddInParameter(dbCommand, "@CODCharge", DbType.Currency, a_Order.CODCharge);
                database.AddInParameter(dbCommand, "@Total", DbType.Currency, a_Order.Total);
                database.AddInParameter(dbCommand, "@Stage", DbType.AnsiString, a_Order.Stage);
                database.AddInParameter(dbCommand, "@Status", DbType.AnsiString, a_Order.Status);
                database.AddInParameter(dbCommand, "@StatusPayment", DbType.Byte, a_Order.StatusPayment);
                database.AddInParameter(dbCommand, "@StatusShipping", DbType.Byte, a_Order.StatusShipping);
                database.AddInParameter(dbCommand, "@BackOrderCount", DbType.Int32, a_Order.BackOrderCount);
                database.AddInParameter(dbCommand, "@IsDelete", DbType.Boolean, a_Order.IsDelete);
                database.AddInParameter(dbCommand, "@DomainID", DbType.Int32, a_Order.DomainID);
                database.AddInParameter(dbCommand, "@ApprovalCode", DbType.AnsiString, a_Order.ApprovalCode);
                database.AddInParameter(dbCommand, "@TransID", DbType.AnsiString, a_Order.TransID);
                database.AddInParameter(dbCommand, "@AVSCode", DbType.AnsiString, a_Order.AVSCode);
                database.AddInParameter(dbCommand, "@SecCode", DbType.AnsiString, a_Order.SecCode);
                database.AddInParameter(dbCommand, "@NumOfRetry", DbType.Int32, a_Order.NumOfRetry);
                database.AddInParameter(dbCommand, "@PayPal_PayerEmail", DbType.AnsiString, a_Order.PayPal_PayerEmail);
                database.AddInParameter(dbCommand, "@IsPending", DbType.Boolean, a_Order.IsPending);
                database.AddInParameter(dbCommand, "@HasNote", DbType.Boolean, a_Order.HasNote);
                database.AddInParameter(dbCommand, "@ShipAsGift", DbType.Boolean, a_Order.ShipAsGift);
                database.AddInParameter(dbCommand, "@Discount", DbType.Currency, a_Order.Discount);
                database.AddInParameter(dbCommand, "@CouponValueType", DbType.Boolean, a_Order.CouponValueType);
                database.AddInParameter(dbCommand, "@CouponValue", DbType.Int32, a_Order.CouponValue);
                database.AddInParameter(dbCommand, "@Typecoupon", DbType.Int32, a_Order.Typecoupon);
                database.AddInParameter(dbCommand, "@CouponID", DbType.AnsiString, a_Order.CouponID);
                database.AddInParameter(dbCommand, "@NofDiscount", DbType.Int32, a_Order.NofDiscount);
                database.AddInParameter(dbCommand, "@IsDeclined", DbType.Boolean, a_Order.IsDeclined);
                database.AddInParameter(dbCommand, "@UserID_Approved", DbType.Int32, a_Order.UserID_Approved);
                database.AddInParameter(dbCommand, "@AVSZip", DbType.AnsiString, a_Order.AVSZip);
                database.AddInParameter(dbCommand, "@ProcessStatus", DbType.Int32, a_Order.ProcessStatus);
                database.AddInParameter(dbCommand, "@MerchantStatus", DbType.Int32, a_Order.MerchantStatus);
                database.AddInParameter(dbCommand, "@LotteryWon", DbType.Boolean, a_Order.LotteryWon);
                database.AddInParameter(dbCommand, "@LotteryNum", DbType.AnsiString, a_Order.LotteryNum);
                database.AddOutParameter(dbCommand, "@OID", DbType.Int32, 0);
                
                database.ExecuteNonQuery(dbCommand);
                a_Order.OID = (int)database.GetParameterValue(dbCommand, "@OID");
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        #endregion

        #region Updatea_Order methods
        
        public virtual void Updatea_Order(a_Order a_Order)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_Orders_upd");            
                
                database.AddInParameter(dbCommand, "@OID", DbType.Int32, a_Order.OID);
                database.AddInParameter(dbCommand, "@DomSaleOrderNo", DbType.AnsiString, a_Order.DomSaleOrderNo);
                database.AddInParameter(dbCommand, "@CustomerID_UserID", DbType.Int32, a_Order.CustomerID_UserID);
                database.AddInParameter(dbCommand, "@Billing_AddrID", DbType.Int32, a_Order.Billing_AddrID);
                database.AddInParameter(dbCommand, "@Shipping_AddrID", DbType.Int32, a_Order.Shipping_AddrID);
                database.AddInParameter(dbCommand, "@OrderDate", DbType.DateTime, a_Order.OrderDate);
                database.AddInParameter(dbCommand, "@ModifiedDate", DbType.DateTime, a_Order.ModifiedDate);
                database.AddInParameter(dbCommand, "@ShipMethodID", DbType.Int32, a_Order.ShipMethodID);
                database.AddInParameter(dbCommand, "@ShippingOptions", DbType.Boolean, a_Order.ShippingOptions);
                database.AddInParameter(dbCommand, "@InstrumentType", DbType.Byte, a_Order.InstrumentType);
                database.AddInParameter(dbCommand, "@InstrumentID", DbType.Int32, a_Order.InstrumentID);
                database.AddInParameter(dbCommand, "@SubTotal", DbType.Currency, a_Order.SubTotal);
                database.AddInParameter(dbCommand, "@Tax", DbType.Currency, a_Order.Tax);
                database.AddInParameter(dbCommand, "@PromoTaxCredit", DbType.Currency, a_Order.PromoTaxCredit);
                database.AddInParameter(dbCommand, "@Shipping_Weight", DbType.Int32, a_Order.Shipping_Weight);
                database.AddInParameter(dbCommand, "@Shipping_Handling", DbType.Currency, a_Order.Shipping_Handling);
                database.AddInParameter(dbCommand, "@PromoShippingCredit", DbType.Currency, a_Order.PromoShippingCredit);
                database.AddInParameter(dbCommand, "@ShipChargeNotIncluded", DbType.Boolean, a_Order.ShipChargeNotIncluded);
                database.AddInParameter(dbCommand, "@CouponCredit", DbType.Currency, a_Order.CouponCredit);
                database.AddInParameter(dbCommand, "@UserCredit", DbType.Currency, a_Order.UserCredit);
                database.AddInParameter(dbCommand, "@CODCharge", DbType.Currency, a_Order.CODCharge);
                database.AddInParameter(dbCommand, "@Total", DbType.Currency, a_Order.Total);
                database.AddInParameter(dbCommand, "@Stage", DbType.AnsiString, a_Order.Stage);
                database.AddInParameter(dbCommand, "@Status", DbType.AnsiString, a_Order.Status);
                database.AddInParameter(dbCommand, "@StatusPayment", DbType.Byte, a_Order.StatusPayment);
                database.AddInParameter(dbCommand, "@StatusShipping", DbType.Byte, a_Order.StatusShipping);
                database.AddInParameter(dbCommand, "@BackOrderCount", DbType.Int32, a_Order.BackOrderCount);
                database.AddInParameter(dbCommand, "@IsDelete", DbType.Boolean, a_Order.IsDelete);
                database.AddInParameter(dbCommand, "@DomainID", DbType.Int32, a_Order.DomainID);
                database.AddInParameter(dbCommand, "@ApprovalCode", DbType.AnsiString, a_Order.ApprovalCode);
                database.AddInParameter(dbCommand, "@TransID", DbType.AnsiString, a_Order.TransID);
                database.AddInParameter(dbCommand, "@AVSCode", DbType.AnsiString, a_Order.AVSCode);
                database.AddInParameter(dbCommand, "@SecCode", DbType.AnsiString, a_Order.SecCode);
                database.AddInParameter(dbCommand, "@NumOfRetry", DbType.Int32, a_Order.NumOfRetry);
                database.AddInParameter(dbCommand, "@PayPal_PayerEmail", DbType.AnsiString, a_Order.PayPal_PayerEmail);
                database.AddInParameter(dbCommand, "@IsPending", DbType.Boolean, a_Order.IsPending);
                database.AddInParameter(dbCommand, "@HasNote", DbType.Boolean, a_Order.HasNote);
                database.AddInParameter(dbCommand, "@ShipAsGift", DbType.Boolean, a_Order.ShipAsGift);
                database.AddInParameter(dbCommand, "@Discount", DbType.Currency, a_Order.Discount);
                database.AddInParameter(dbCommand, "@CouponValueType", DbType.Boolean, a_Order.CouponValueType);
                database.AddInParameter(dbCommand, "@CouponValue", DbType.Int32, a_Order.CouponValue);
                database.AddInParameter(dbCommand, "@Typecoupon", DbType.Int32, a_Order.Typecoupon);
                database.AddInParameter(dbCommand, "@CouponID", DbType.AnsiString, a_Order.CouponID);
                database.AddInParameter(dbCommand, "@NofDiscount", DbType.Int32, a_Order.NofDiscount);
                database.AddInParameter(dbCommand, "@IsDeclined", DbType.Boolean, a_Order.IsDeclined);
                database.AddInParameter(dbCommand, "@UserID_Approved", DbType.Int32, a_Order.UserID_Approved);
                database.AddInParameter(dbCommand, "@AVSZip", DbType.AnsiString, a_Order.AVSZip);
                database.AddInParameter(dbCommand, "@ProcessStatus", DbType.Int32, a_Order.ProcessStatus);
                database.AddInParameter(dbCommand, "@MerchantStatus", DbType.Int32, a_Order.MerchantStatus);
                database.AddInParameter(dbCommand, "@LotteryWon", DbType.Boolean, a_Order.LotteryWon);
                database.AddInParameter(dbCommand, "@LotteryNum", DbType.AnsiString, a_Order.LotteryNum);
                
                database.ExecuteNonQuery(dbCommand);
            }
            catch (Exception ex)
            {
               throw ex;
            }
        }
        
        #endregion

        #region Deletea_Order methods
        public virtual void Deletea_Order(int oid)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_Orders_del");
                
                database.AddInParameter(dbCommand, "@OID", DbType.Int32, oid);
                
                database.ExecuteNonQuery(dbCommand);
            }
            catch (Exception ex)
            {
               throw ex;
            }
        }

        #endregion

        #region Geta_Order methods
        
        public virtual a_Order Geta_Order(int oid)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_Orders_sel");
                
                database.AddInParameter(dbCommand, "@OID", DbType.Int32, oid);
                
                a_Order a_Order =  null;
                using (IDataReader reader = database.ExecuteReader(dbCommand))
                {
                    if (reader.Read())
                    {
                        a_Order = Createa_OrderFromReader(reader);
                        reader.Close();
                    }
                }
                return a_Order;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        
        #endregion

        #region Geta_OrderList methods
        public virtual a_OrderCollection Geta_OrderList(a_OrderColumns orderBy, OrderDirection orderDirection, int page, int pageSize, out int totalRecords)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_Orders_selList");
                
                database.AddInParameter(dbCommand, "@OrderBy", DbType.AnsiString, orderBy.ToString());
                database.AddInParameter(dbCommand, "@OrderDirection", DbType.AnsiString, orderDirection.ToString());
                database.AddInParameter(dbCommand, "@Page", DbType.Int32, page);
                database.AddInParameter(dbCommand, "@PageSize", DbType.Int32, pageSize);
                database.AddOutParameter(dbCommand, "@TotalRecords", DbType.Int32, 4);
                
                a_OrderCollection a_OrderCollection = new a_OrderCollection();
                using (IDataReader reader = database.ExecuteReader(dbCommand))
                {
                    while (reader.Read())
                    {
                        a_Order a_Order = Createa_OrderFromReader(reader);
                        a_OrderCollection.Add(a_Order);
                    }
                    reader.Close();
                }
                totalRecords = (int)database.GetParameterValue(dbCommand, "@TotalRecords");
                return a_OrderCollection;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        
        public virtual a_OrderCollection Geta_OrderList(a_OrderColumns orderBy, OrderDirection orderDirection)
        {            
            int totalRecords = 0;
            return Geta_OrderList(orderBy, orderDirection, 0, 0, out totalRecords);
        }
        
        #endregion
    }
}